package com.bocom.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.bocom.entity.Users;
import com.bocom.service.UserService;

@Controller
@SessionAttributes("user")
public class UserController {
	
    @Resource(name="userService")
    private UserService service;
	
	@RequestMapping(value = "/Login", method = RequestMethod.POST)
	public String doLogin(@RequestParam String username , @RequestParam String password, HttpServletRequest request, ModelMap map ){
		
		try {
			Users user = service.doLogin(username, password); 
			map.put("user", user);
		} catch (Exception e) {
			request.setAttribute("error", e.getMessage());
			return "index.jsp";
		}
		
		//	System.out.println("UserController username="+username);
		//	System.out.println("UserController passwrod="+password);
		
		return "/WEB-INF/jsp/loginform.jsp";
	}
	
	@RequestMapping("doLogout")
	public String doLogout(SessionStatus status){
		status.setComplete();
		return "index.jsp";
	}
	
}
